package pl.edu.agh.mobsys.utils;

import java.util.List;

import pl.edu.agh.mobsys.R;
import pl.edu.agh.mobsys.data.Message;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

/**
 * Adapter dla elementów listy wiadomości chatu osobowego.
 * */
public class MessagesAdapter extends BaseAdapter {

	private static final String CLASSTAG = MessagesAdapter.class
			.getSimpleName();
	private final List<Message> messages;

	private final Context ctx;

	public MessagesAdapter(Context context, List<Message> messages) {
		ctx = context;
		this.messages = messages;
	}

	public int getCount() {
		return messages.size();
	}

	public Object getItem(int position) {
		return messages.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		Message m = messages.get(position);
		View v = convertView;
		if (v == null) {
			LayoutInflater vi = (LayoutInflater) ctx
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = vi.inflate(R.layout.chat_row, null);
		}
		try {
			((TextView) v.findViewById(R.id.chat_row_username))
					.setText(m.author);
			((TextView) v.findViewById(R.id.chat_row_time)).setText(m.time);
			((TextView) v.findViewById(R.id.chat_row_message))
					.setText(m.content);
		} catch (Exception e) {
			e.printStackTrace();
		}
		v.setLongClickable(true);
		return v;
	}
}
